package com.xiaocai.myblog.services;

import com.xiaocai.myblog.domain.User;
import com.xiaocai.myblog.enums.ErrorCodeEnum;
import com.xiaocai.myblog.exception.BusinessException;
import com.xiaocai.myblog.mapper.UserDao;
import com.xiaocai.myblog.utils.RedisUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Calendar;
import java.util.Date;


/**
 * @author chenhefu
 * @date 2019-09-27 22:05
 */
@Service
public class UserService {

    @Autowired
    private UserDao userDao;
    @Autowired
    private RedisUtil redisUtil;

    public User getUserById(Integer id){
       return userDao.getUserById(id);
    }

    public User login(String email, String password){

        User user = userDao.login(email, password);
        if (user == null){
            throw new BusinessException(ErrorCodeEnum.SERVER_ERROR_PARAM,"用户名或者密码错误");
        }
        return user;
    }





    public boolean register(User user){
        if (isRegistered(user.getEmail())){
            throw new BusinessException(ErrorCodeEnum.BUSINESS_ERROR,"该邮箱已经注册过,请换个邮箱");
        }
        return userDao.register(user) > 0;
    }

    private boolean isRegistered(String email){
        return userDao.getUserByEmail(email) != null;
    }

    public static void main(String[] args) {

    }
}
